import {body, description, path, request, summary, tagsAll} from "koa-swagger-decorator-fork";
import {getTags,addTags,delTag,getFileTag,tagFile,untagFile} from '../../utils/nc/tag'
const TagsSchema = {
    tags: { type: 'array', required: true,  items: 'string'}
}

@tagsAll(['TagFile'])
export default class TagFileRouter {
    @request('GET', '/tags')
    @summary('find tags')
    @description('查询标签列表')
    static async findTags(ctx) {
        let tags = await getTags(ctx.client,"/systemtags")
        ctx.body = tags
    }

    @request('POST', '/tags')
    @summary('add tags')
    @description('增加标签')
    @body(TagsSchema)
    static async addTags(ctx) {
        let {tags} = ctx.request.body
        await addTags(ctx.client,tags)
        ctx.body = {}
    }

    @request('DELETE', '/tags/{id}')
    @summary('delete tag by tag id')
    @description('删除标签')
    @path({ id: { type: 'string', required: true } })
    static async delTags(ctx) {
        await delTag(ctx.client,ctx.params.id)
        ctx.body = {}
    }

    @request('GET', '/files/{fileid}/tags')
    @summary('get file tags by file id')
    @description('获取指定文件的标签')
    @path({ fileid: { type: 'string', required: true } })
    static async getFileTag(ctx) {
        let tags = await getFileTag(ctx.client,ctx.params.fileid)
        ctx.body = tags
    }

    @request('POST', '/files/{fileid}/tags')
    @summary('tag file by file id')
    @description('指定文件打标签')
    @path({ fileid: { type: 'string', required: true } })
    @body(TagsSchema)
    static async tagFile(ctx) {
        let {fileid} = ctx.params,{tags} = ctx.request.body
        await tagFile(ctx.client,fileid,tags)
        ctx.body = {}
    }

    @request('DELETE', '/files/{fileid}/tags')
    @summary('untag file by file id')
    @description('指定文件移除标签')
    @path({ fileid: { type: 'string', required: true } })
    @body(TagsSchema)
    static async untagFile(ctx) {
        let {fileid} = ctx.params,{tags} = ctx.request.body
        await untagFile(ctx.client,fileid,tags)
        ctx.body = {}
    }
}